import store from '../../../../../../store'
import api from '../../../../../../utils/api.js'
import request from '../../../../../../utils/request.js'
const add = {
	// 页面数据
	data: {
		companyId: store.state.company.companyId, // 公司id
		// 新增专家
		selectMenberName: '请选择专家', // 专家名字
		userId: '', // 所选择的专家ID
		expertId: '', // 修改所需要的专家ID
		// 擅长领域

		fieldRelation: [], // 已存在的擅长领域-----已选中
		customizeFieldRelation: [], // 自定义擅长领域-----已选中
		// 教育经历
		educational: [],
		// 职称等级
		rankName: [],
		introduce: '', // 个人简介
		// 获奖成果
		awardWinning: [],
		// 培训经历
		trainingExperience: [],
		// 社会职务
		socialPosition: [],
		// 主持在研项目
		projectsStudy: [],
		// 代表文章
		delegateArticle: []
	},
	fieldRelationList: [], // 已存在的擅长领域-----所有
	fieldRelations: [], // 已存在的擅长领域-----页面数据绑定
	// newFieldRelations: [],// 已存在的擅长领域-----页面数据绑定
	customList: [], // 自定义擅长领域-----所有

	// 选择成员
	selectMenber: (row) => {
		that.userId = row.userId
		that.selectMenberName = row.name
	},
	// 擅长领域方法
	territory: {
		// 内置擅长领域
		internally: (arr) => {
			that.fieldRelation = []
			if (arr.length > 0) {
				arr.forEach((v) => {
					self.fieldRelationList.filter(item => {
						if (item.id == v) {
							let data = {
								id: item.id,
								fieldName: item.fieldName
							}
							that.fieldRelation.push(data)
						}
					})
				})
			}
		},
		// 选择内置擅长领域 ------------------- 新
		newInternally: (data, callBack) => {
			if (that.fieldRelation.length < 1) {
				that.fieldRelation.push(data)
				callBack(true)
			} else {
				let isExist = that.fieldRelation.some(item => {
					return item.id == data.id
				})
				if (isExist) {
					callBack(false)
				} else {
					that.fieldRelation.push(data)
					callBack(true)
				}
			}
		},
		// 自定义添加擅长领域
		custom: (data) => {
			self.customList.push(data)
			that.customizeFieldRelation.push(data.fieldName)
		},
		// 自定义添加擅长领域 ------------------- 新
		newCustom: (data, callBack) => {
			if (that.customizeFieldRelation.length < 1) {
				that.customizeFieldRelation.push(data)
				callBack(true)
			} else {
				let isExist = that.customizeFieldRelation.some(item => {
					return item.fieldName == data.fieldName
				})
				if (isExist) {
					callBack(false)
				} else {
					that.customizeFieldRelation.push(data)
					callBack(true)
				}
			}
		},
		// 删除擅长领域
		removeTerritory: (type, index, callBack) => {
			// type: 1为删除系统内置领域，0为删除自定义领域
			if (type === '1') {
				that.fieldRelation.splice(index, 1)
				callBack(true)
			} else if (type === '0') {
				that.customizeFieldRelation.splice(index, 1)
				callBack(true)
			} else {
				callBack(false)
				throw new TypeError('type参数错误：1为删除系统内置领域，0为删除自定义领域')
			}
		}
	},
	// 添加教育经历
	education: {
		// 添加
		addition: (data) => {
			that.educational.push(data)
		},
		// 获取教育经历数据
		getEducation: (index, callBack) => {
			callBack(that.educational[index])
		},
		// 修改
		update: (data, index) => {
			that.educational.splice(index, 1, data)
		},
		// 删除
		remove: (index) => {
			that.educational.splice(index, 1)
		}
	},
	// 职称等级
	rankname: {
		// 添加
		addition: (data) => {
			that.rankName.push(data)
		},
		// 根据下标获取数据
		getSelf: (index, callBack) => {
			callBack(that.rankName[index])
		},
		// 修改
		update: (data, index) => {
			that.rankName.splice(index, 1, data)
		},
		// 删除
		remove: (index) => {
			that.rankName.splice(index, 1)
		}
	},
	// 获奖成果
	awardWinning: {
		// 添加
		addition: (data) => {
			that.awardWinning.push(data)
		},
		// 根据下标获取数据
		getSelf: (index, callBack) => {
			callBack(that.awardWinning[index])
		},
		// 修改
		update: (data, index) => {
			that.awardWinning.splice(index, 1, data)
		},
		// 删除
		remove: (index) => {
			that.awardWinning.splice(index, 1)
		}
	},
	// 培训经历
	training: {
		// 添加
		addition: (data) => {
			that.trainingExperience.push(data)
		},
		// 根据下标获取数据
		getSelf: (index, callBack) => {
			callBack(that.trainingExperience[index])
		},
		// 修改
		update: (data, index) => {
			that.trainingExperience.splice(index, 1, data)
		},
		// 删除
		remove: (index) => {
			that.trainingExperience.splice(index, 1)
		}
	},
	// 社会职务
	social: {
		// 添加
		addition: (data) => {
			that.socialPosition.push(data)
		},
		// 根据下标获取数据
		getSelf: (index, callBack) => {
			callBack(that.socialPosition[index])
		},
		// 修改
		update: (data, index) => {
			that.socialPosition.splice(index, 1, data)
		},
		// 删除
		remove: (index) => {
			that.socialPosition.splice(index, 1)
		}
	},
	// 主持在研项目
	research: {
		// 添加
		addition: (data) => {
			that.projectsStudy.push(data)
		},
		// 根据下标获取数据
		getSelf: (index, callBack) => {
			callBack(that.projectsStudy[index])
		},
		// 修改
		update: (data, index) => {
			that.projectsStudy.splice(index, 1, data)
		},
		// 删除
		remove: (index) => {
			that.projectsStudy.splice(index, 1)
		}
	},
	// 代表文章
	article: {
		// 添加
		addition: (data) => {
			that.delegateArticle.push(data)
		},
		// 根据下标获取数据
		getSelf: (index, callBack) => {
			callBack(that.delegateArticle[index])
		},
		// 修改
		update: (data, index) => {
			that.delegateArticle.splice(index, 1, data)
		},
		// 删除
		remove: (index) => {
			that.delegateArticle.splice(index, 1)
		}
	},
	// 初始页面函数
	init: {
		getTerritory: () => {
			// 获取内置擅长领域
			request.postForm(api.getFieldRelation).then(res => {
				if (res.code == 200) {
					self.fieldRelationList = res.data
				}
			})
		},
		clear: () => {
			// 数据初始化
			that.selectMenberName = '请选择专家'
			that.userId = ''

			self.fieldRelationList = []
			self.fieldRelations = []
			self.customList = []



			that.fieldRelation = []
			that.customizeFieldRelation = []

			that.educational = []
			that.rankName = []
			that.introduce = ''
			that.awardWinning = []
			that.trainingExperience = []
			that.socialPosition = []
			that.projectsStudy = []
			that.delegateArticle = []
		},
		getExpert: (id, callBack) => {
			// 获取专家详情
			request.postForm(api.queryExpertDetails, {
				expertId: id
			}).then(res => {
				if (res.code == 200) {
					// 给页面数据赋值
					let data = res.data
					that.selectMenberName = data.basicInformation[0].expertName // 专家名
					that.expertId = data.basicInformation[0].expertId // 专家ID
					// 擅长领域
					if (data.field && data.field.length > 0) {
						that.fieldRelation = []
						self.fieldRelations = []
						data.field.forEach((v) => {
							if (self.fieldRelationList && self.fieldRelationList.length > 0) {
								self.fieldRelationList.filter(item => {
									if (item.id == v.fieldId) {
										let data = {
											id: item.id,
											fieldName: item.fieldName
										}
										that.fieldRelation.push(data)
									}
								})
							}

							self.fieldRelations.push(v.fieldId)
						})
					}
					// 教育经历
					that.educational = data.educational
					// 职称等级
					that.rankName = data.rankName
					// 个人简介
					if (data.introduce && data.introduce.length > 0) {
						that.introduce = data.introduce[0].introduceContent
					}
					// 获奖成果
					that.awardWinning = data.awardWinning
					// 培训经历
					that.trainingExperience = data.trainingExperience
					// 社会职务
					that.socialPosition = data.socialPosition
					// 主持在研项目
					that.projectsStudy = data.projectsStudy
					// 代表文章
					that.delegateArticle = data.delegateArticle

					setTimeout(() => {
						callBack(true)
					}, 200)
				}
			})
		}
	},
	uploadFile: (file, callBack) => {
		// 上传文件
		let formData = new FormData()
		formData.append('photo', file)
		request.uploading(api.expertUploadImg, formData).then(res => {
			if (res.code == 200) {
				callBack(res.data)
			}
		})
	}

}

const that = add.data
const self = add
export default add
